//
//  FitureCustomPresentManager.swift
//  taurus
//
//  Created by 刘艺 on 2021/8/16.
//  Copyright © 2021 com.fiture. All rights reserved.
//

import UIKit

class FitureCustomPresentManager {
    
    private var presentingVC: UIViewController?
    private var presentedVC: FiturePresentContainerViewController = FiturePresentContainerViewController()
    private var presentConfig: FitureCustomPresentConfig = FitureCustomPresentConfig()
    
    /// ps：暂未使用
    var delegate: FiturePresentContainerVCDismissDelegate? {
        get {
            self.presentedVC.delegate
        }
        set {
            self.presentedVC.delegate = newValue
        }
    }

    /// 通过ViewController初始化manager
    ///
    /// - Parameters:
    ///     - addPresentingViewController: 执行模态的vc（presenting vc ）
    ///     - addPresentedViewController: 被模态的vc（presented vc）
    ///     - config:present 相关配置
    init(addPresentingViewController presentingVC: UIViewController,
         addPresentedViewController presentedVC: UIViewController,
         presentConfig  config: FitureCustomPresentConfig = FitureCustomPresentConfig()) {
        self.presentingVC = presentingVC
        presentConfig = config
        configContainer(vc: presentedVC, config: config)
    }
    
    /// 通过UIView初始化manager
    ///
    /// - Parameters:
    ///     - addPresentingViewController: 执行模态的vc（presenting vc ）
    ///     - addPresentedView: 被模态的view（presented view）
    ///     - config:present 相关配置
    init(addPresentingViewController presentingVC: UIViewController,
         addPresentedView view: UIView,
         presentConfig config: FitureCustomPresentConfig = FitureCustomPresentConfig()) {
        self.presentingVC = presentingVC
        presentConfig = config
        configContainer(view: view, config: config)
    }

    private func configContainer(vc: UIViewController, config: FitureCustomPresentConfig) {
        presentedVC.configContainer(childrenViewController: vc, config: config)
    }

    private func configContainer(view: UIView, config: FitureCustomPresentConfig) {
        presentedVC.configContainer(subView: view, config: config)
    }
    
    /// 执行模态
    ///
    /// - Parameters:
    ///     - animated: 模态动画
    ///     - completion: 模态结束block
    public func present(animated: Bool, completion:(() -> Void)?) {
        presentedVC.modalPresentationStyle = .overFullScreen
        if presentConfig.needWrapNav {
            let navi = UINavigationController(rootViewController: presentedVC)
            navi.transitioningDelegate = presentedVC
            navi.modalPresentationStyle = .overFullScreen
            presentingVC?.present(navi, animated: animated, completion: completion)
        } else {
            presentedVC.transitioningDelegate = presentedVC
            presentingVC?.present(presentedVC, animated: animated, completion: completion)
        }
    }
    
    
    /// 执行dismiss
    ///
    /// 推荐使用manager提供的dismiss方法，系统提供的dismiss不能触发自定义的FiturePresentContainerVCDismissDelegate
    ///
    /// - Parameters:
    ///     - subObject: 调用dismiss的视图/或者视图控制器
//    class func dismiss(by subObject: AnyObject) {
//        var containerVC: FiturePresentContainerViewController?
//        if subObject.isKind(of: UIView.self) == true {
//            if let view = subObject as? UIView {
//                containerVC = view.parentContainerViewController() as? FiturePresentContainerViewController
//                containerVC?.customDismiss()
//            }
//            return
//        }
//
//        if subObject.isKind(of: UIViewController.self) == true {
//            if let vc = subObject as? UIViewController {
//                containerVC = vc.view.parentContainerViewController() as? FiturePresentContainerViewController
//                containerVC?.customDismiss()
//            }
//            return
//        }
//    }
}

struct FitureCustomPresentConfig {
    var title = ""
    /// 是否给presented container vc 子控制器添加导航栏
    var needNav = false
    /// 是否给presented container vc  控制器添加导航栏
    var needWrapNav = false
    var needCloseBtn = true
    var needSeparaLine = true
    /// 是否全屏模态显示,为fullScreen时，customHeightStyle设置失效
    var modalPresentationStyle: CustomModalPresentationStyle = .halfScreen /// 暂时未用
    /// 自定义模态容器高度。默认defaultContainerHeight1，屏幕高度0.9。
    var customHeightStyle: CusomModalPresentionHalfScreenHeightStyle = .defaultContainerHeight1()
    var maskColor: UIColor = UIColor.black
    var maskColorAlpha: CGFloat = 0.2

    /// presented容器VC高度
    var containerViewControllerHeight: CGFloat {

        if modalPresentationStyle == .fullScreen {
            return UIScreen.main.bounds.size.height
        }
        
        switch customHeightStyle {
        case .defaultContainerHeight1(let height):
            return height
        case .defaultContainerHeight2(let height):
            return height
        case .defaultContainerHeight3(let height):
            return height
        case .customContainerHeight(let height):
            return height > UIScreen.main.bounds.size.height ? 0.9 * UIScreen.main.bounds.size.height : height
        }
    }

    enum CustomModalPresentationStyle: Int {
        case fullScreen = 1
        case halfScreen = 2
    }

    enum CusomModalPresentionHalfScreenHeightStyle {
        case defaultContainerHeight1(CGFloat = 0.9 * UIScreen.main.bounds.size.height)
        case defaultContainerHeight2(CGFloat = 0.7 * UIScreen.main.bounds.size.height)
        case defaultContainerHeight3(CGFloat = 0.5 * UIScreen.main.bounds.size.height)
        case customContainerHeight(CGFloat = 0.9 * UIScreen.main.bounds.size.height)
    }
}

